<?php
namespace app\exception;

use Throwable;
use Tinywan\Jwt\Exception\JwtTokenException;
use Tinywan\Jwt\Exception\JwtTokenExpiredException;
use Webman\Exception\ExceptionHandler;
use Webman\Http\Request;
use Webman\Http\Response;

class Handler extends ExceptionHandler
{

    public function report(Throwable $exception)
    {
        parent::report($exception);
    }

    public function render(Request $request, Throwable $exception): Response
    {
        $path=$request->path();
        if(substr($path,0,4)==='/api'){
            if($exception instanceof JwtTokenException || $exception instanceof JwtTokenExpiredException){
                return json([
                    'code'=>401,
                    'message'=>'Unauthorized',
                    'timestamp'=>time()
                ]);
            }
            return json([
                'code'=>$exception->getCode()??500,
                'message'=>$exception->getMessage(),
                'timestamp'=>time()
            ]);
        }

        return parent::render($request,$exception);
    }
}